import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate"

Vue.use(Vuex);

export default new Vuex.Store({
    plugins: [createPersistedState({ reducer(val) {
	return {collectCards: val.collectCards,count: val.count}}})],
    state: {
        'collectCards': [],
        'count': 0,
	'history':[],
    },

    mutations: {
	push_history(state,o) {
	    for(var j = 0,len = state.history.length; j < len; j++){
		if(state.history[j].title==o.title)return;
	    }
	    state.history.push(o);
	},
	saveCollect(state,o) {
	    state.collectCards=o;
	},
	saveCount(state,n) {
	    state.count=n;
	},
	saveCard(state,o){
	    for(var j = 0,len = state.collectCards.length; j < len; j++){
		if(state.collectCards[j].id==o.id )
		    state.collectCards[j]==o;
	    }
	},
	clearHistory(state){
	    state.history=[];
	    console.log("clear");
	}
    }
})
